package org.qloudgen.borg.pages.panel;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.qloudgen.borg.CommonValues;
import org.qloudgen.borg.beans.FuncBean;
import org.qloudgen.borg.beans.ResultBean;

import com.google.common.collect.Lists;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class AddFuncResultPanel extends Panel {

	private static final long serialVersionUID = 1L;

	public AddFuncResultPanel(String id, final FuncBean func , final ModalWindow window) {
		
		super(id);
		
		final Form<?> form = new Form("form",new CompoundPropertyModel<ResultBean>(new ResultBean())) {
			
			private static final long serialVersionUID = 1L;

			protected void onSubmit() {
				ResultBean column = (ResultBean)getDefaultModelObject();
				func.getColumns().add(new ResultBean().setColumnCode(column.getColumnCode())
						                            .setColumnName(column.getColumnName())
						                            .setIsOrder(column.getIsOrder()));
			}
			
		};
		this.add(form);
		
		form.add(new RequiredTextField<String>("columnCode"));
		form.add(new RequiredTextField<String>("columnName"));
		
		IChoiceRenderer renderer = new ChoiceRenderer() {
			private static final long serialVersionUID = 1L;
			public Object getDisplayValue(Object object) {
				return CommonValues.isOrders.get(object);
			}
		};
		form.add(new DropDownChoice("isOrder",Lists.newArrayList(CommonValues.isOrders.keySet()),renderer));
		
		// button submit 
		form.add(new AjaxButton("confirm"){

			private static final long serialVersionUID = 1L;
			protected void onAfterSubmit(AjaxRequestTarget target, Form<?> form) {
				if (window != null)	window.close(target);
			}
		});
		
		// button cancel
		form.add(new AjaxLink<Void>("cancel"){
			
			private static final long serialVersionUID = 1L;
			public void onClick(AjaxRequestTarget target) {
				if (window != null)	window.close(target);
			}});

	}

}
